﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using HidApi;

namespace OsuKeyboard
{
	public partial class FormMain : Form
    {
		public UInt16 DeviceVer { get { return HidAPI.devAttr.VER; } }   //获取设备版本号
        public FormMain()
        {
            InitializeComponent();
        }
		#region 主窗体功能
		public IntPtr[] ChildrenFrom = new IntPtr[5];
        // 窗口加载的时候执行此函数
        private void OsuKeyboard_Load(object sender, EventArgs e)
        {
			UIchange(-1);
			CheckLanguage();
			AddMouseUpEvent();
			UpdateCheck.Start();

			HidAPI.DeviceRemoved += HidDeviceRemoved;
			HidAPI.DataReceived += HidDataReceived;
			if (HidAPI.LinkOsuKeypad()) DeviceConnected();
		}
        // 根据区域语言显示对应的界面语言
        private void CheckLanguage()
        {
            NoLink.Text = Languages.GetLang(NoLink.Name);
            KeySet.Text = Languages.GetLang(KeySet.Name);
            labelDebounce.Text = Languages.GetLang(labelDebounce.Name);
            LedSet.Text = Languages.GetLang(LedSet.Name);
            L1Mode0.Text = Languages.GetLang(L1Mode0.Name);
            L1Mode1.Text = Languages.GetLang(L1Mode1.Name);
            L1Mode2.Text = Languages.GetLang(L1Mode2.Name);
            L1Mode3.Text = Languages.GetLang(L1Mode3.Name);
            L2Mode0.Text = Languages.GetLang(L2Mode0.Name);
            L2Mode1.Text = Languages.GetLang(L2Mode1.Name);
            L2Mode2.Text = Languages.GetLang(L2Mode2.Name);
            L2Mode3.Text = Languages.GetLang(L2Mode3.Name);
            L3Mode0.Text = Languages.GetLang(L3Mode0.Name);
            L3Mode3.Text = Languages.GetLang(L3Mode3.Name);
        }
        // 根据触盘版本修改界面
        private void UIchange(int Version)
        {
            switch (Version)
            {
                case 1:
                    LED1Mode.Size = new Size(86, 86);
                    LED2Mode.Size = new Size(86, 86);
                    LED3Mode.Visible = false;

                    Button3.Size = new Size(Button3.Location.X - Button5.Location.X + Button3.Size.Width, Button5.Size.Height);
                    Button3.Font = new Font(Button3.Font.FontFamily, 15.75F);
                    Button3.Location = Button5.Location;
                    label4.Visible = false;
                    Button4Code.Visible = false;
                    label5.Visible = false;
                    Button5Code.Visible = false;
                    labelDebounce.Visible = false;
                    Debounce.Visible = false;

                    tabControl.Visible = true;
                    break;
                case 2:
                    LED1Mode.Size = new Size(86, 86);
                    LED2Mode.Size = new Size(86, 86);
                    LED3Mode.Visible = false;

                    Button3.Location = new Point(Button4.Location.X * 2 - Button5.Location.X, Button4.Location.Y);
                    Button3.Size = Button4.Size;
                    Button3.Font = new Font(Button3.Font.FontFamily, 12F);
                    label4.Visible = true;
                    Button4Code.Visible = true;
                    label5.Visible = true;
                    Button5Code.Visible = true;
                    labelDebounce.Visible = false;
                    Debounce.Visible = false;

                    tabControl.Visible = true;
                    break;
                case 3:
                    LED1Mode.Size = new Size(86, 108);
                    LED2Mode.Size = new Size(86, 108);
                    LED3Mode.Visible = true;

                    Button3.Location = new Point(Button4.Location.X * 2 - Button5.Location.X, Button4.Location.Y);
                    Button3.Size = Button4.Size;
                    Button3.Font = new Font(Button3.Font.FontFamily, 12F);
                    label4.Visible = true;
                    Button4Code.Visible = true;
                    label5.Visible = true;
                    Button5Code.Visible = true;
                    labelDebounce.Visible = true;
                    Debounce.Visible = true;

                    tabControl.Visible = true;
                    break;
                default:
                    this.Text = "AmusingKeypad";
                    tabControl.Visible = false;
                    break;
            }
        }
        #endregion
        #region 按键设置功能
        // 定义键值变量
        private string TkValue;
		public string TKValue { set { TkValue = value; } }
		// 定义当前选中的按键
		public Button SelectedBT;
		// 批量控件添加鼠标弹起事件
		private void AddMouseUpEvent()
        {
            foreach (Control BTcode in KeySet.Controls)
            {
                if (BTcode is Button) BTcode.MouseUp += new MouseEventHandler(BT_MouseUp);
            }
        }
        // 鼠标弹起事件处理
        private void BT_MouseUp(object sender, MouseEventArgs e)
        {
            tabControl.Focus(); //转移控件焦点，消除粗黑框
            Button BTcode = sender as Button;
            SelectedBT = BTcode;
            KeySettingsPanel TKDialog = new KeySettingsPanel();
            TKDialog.ShowDialog(this);
            Byte PadCode1 = 0x00;
			if (TkValue.Contains("LCtrl")) PadCode1 |= 0x01;
			if (TkValue.Contains("LShift")) PadCode1 |= 0x02;
			if (TkValue.Contains("LAlt")) PadCode1 |= 0x04;
			if (TkValue.Contains("LWin")) PadCode1 |= 0x08;
			if (TkValue.Contains("RCtrl")) PadCode1 |= 0x10;
			if (TkValue.Contains("RShift")) PadCode1 |= 0x20;
			if (TkValue.Contains("RAlt")) PadCode1 |= 0x40;
			if (TkValue.Contains("RWin")) PadCode1 |= 0x80;
			string[] Tcodes = TkValue.Split('|');
			HidKeyCode.HidKeypadCode.TryGetValue(Tcodes[Tcodes.Length - 1].Trim(), out byte PadCode0);
            if ((PadCode0 | PadCode1) == 0x00) return;

            Byte KeyAddress = 0x1A;
            if (BTcode.Name == "Button1" || BTcode.Name == "Button1Code") KeyAddress = 0x1A;
            if (BTcode.Name == "Button2" || BTcode.Name == "Button2Code") KeyAddress = 0x1B;
            if (BTcode.Name == "Button3" || BTcode.Name == "Button3Code") KeyAddress = 0x1C;
            if (BTcode.Name == "Button4" || BTcode.Name == "Button4Code") KeyAddress = 0x1D;
            if (BTcode.Name == "Button5" || BTcode.Name == "Button5Code") KeyAddress = 0x1E;

			List<byte[]> keyBuffers = new List<byte[]>
			{
				new byte[] { 0x81, 0x00, 0x00, 0x00, 0x00, KeyAddress, 0x00, 0x34, PadCode0, 0x34, PadCode1 }
			};
			AddDataBuffer(keyBuffers);
        }
        // 按键消抖动设置
        private void DebounceUpDown_ValueChanged(object sender, EventArgs e)
        {
			KeyTickTimer.Enabled = true;
		}
		// 定时0.5s检查按键消抖动参数是否改变
		private void KeyTickTimer_Tick(object sender, EventArgs e)
		{
			if (BackPanel.Enabled && Debounce.Value != DevCfg.KeyTick)
			{
				DevCfg.KeyTick = (byte)Debounce.Value;
				AddDataBuffer(new List<byte[]> { DevCfg.FlashCode[0xF, 1] });
			}
			else
			{
				KeyTickTimer.Enabled = false;
			}
			Debug.Print("KeyTickTimer: " + DateTime.Now.ToLongTimeString().ToString());
		}
		#endregion
		#region 灯光功能
		// 定义灯光颜色变量
		private Color LedValue;
        public Color LEDValue
        {
            get { return LedValue; }
            set { LedValue = value; }
        }
        // 定义当前选中的LED
        public Button SelectedLB;
        public Color RealTimeColorLed
        {
            set { SetColor(SelectedLB, value); }
        }
        // LED灯光颜色更改
        private void SetLedButton_MouseUp(object sender, MouseEventArgs e)
        {
            tabControl.Focus();

            Button SelectLB = sender as Button;
            SelectedLB = SelectLB;
            LedValue = SelectLB.BackColor;

            if (SelectLB.Text == "LED1" && L1Mode3.Checked)
            {
                ColorTable CTDialog = new ColorTable();
                CTDialog.ShowDialog(this);
                return;
            }
            if (SelectLB.Text == "LED2" && L2Mode3.Checked)
            {
                ColorTable CTDialog = new ColorTable();
                CTDialog.ShowDialog(this);
                return;
            }
            if (SelectLB.Text == "LED3" && L3Mode3.Checked)
            {
                ColorTable CTDialog = new ColorTable();
                CTDialog.ShowDialog(this);
                return;
            }

            ColorWheelPicker CPDialog = new ColorWheelPicker();
            CPDialog.ShowDialog(this);
            if (LedValue.ToArgb() == SelectLB.BackColor.ToArgb()) return;

            SetColor(SelectLB, LedValue);
        }
        // LED设置按钮字体颜色调节，防止背景色与前景色相近而看不清字体
        private void SetLedButton_BackColorChanged(object sender, EventArgs e)
        {
            Button SelectLB = sender as Button;
            Color Bcolor = SelectLB.BackColor;
            Boolean ColorGam = Bcolor.GetBrightness() > 0.4 && (Bcolor.R + Bcolor.G > 100);
            SelectLB.ForeColor = ColorGam ? Color.Black : Color.White;
            
        }
		// 颜色设置函数
        private void SetColor(Button SelectLB, Color SetColor)
		{
			List<byte[]> colorBuffers = new List<byte[]>();
			if (SelectLB.Text == "LED1")
			{
				DevCfg.LED1_Color = SetColor;
				for (int i = 1; i <= 3; i++) colorBuffers.Add(DevCfg.FlashCode[i, 0]);
			}
			if (SelectLB.Text == "LED2")
			{
				DevCfg.LED2_Color = SetColor;
				for (int i = 4; i <= 6; i++) colorBuffers.Add(DevCfg.FlashCode[i, 0]);
			}
			if (SelectLB.Text == "LED3")
			{
				DevCfg.LED3_Color = SetColor;
				for (int i = 7; i <= 9; i++) colorBuffers.Add(DevCfg.FlashCode[i, 0]);
			}
			AddDataBuffer(colorBuffers);
		}
        // LED灯光模式选择
        private void LedMode_CheckedChanged(object sender, EventArgs e)
        {
			if (tabControl.SelectedIndex != 1) return;  // tabControl选项卡不为1时不触发事件
			RadioButton SelectRB = sender as RadioButton;
            if (SelectRB.Checked == false) return;		// 防止意外出错
			if (SelectRB.Name == "L1Mode0") DevCfg.LED1_Mod = 0xFF;
            if (SelectRB.Name == "L1Mode1") DevCfg.LED1_Mod = 0x01;
            if (SelectRB.Name == "L1Mode2") DevCfg.LED1_Mod = 0x02;
            if (SelectRB.Name == "L1Mode3") DevCfg.LED1_Mod = 0x03;
            if (SelectRB.Name == "L2Mode0") DevCfg.LED2_Mod = 0xFF;
            if (SelectRB.Name == "L2Mode1") DevCfg.LED2_Mod = 0x01;
            if (SelectRB.Name == "L2Mode2") DevCfg.LED2_Mod = 0x02;
			if (SelectRB.Name == "L2Mode3") DevCfg.LED2_Mod = 0x03;
			if (SelectRB.Name == "L3Mode0") DevCfg.LED3_Mod = 0xFF;
			if (SelectRB.Name == "L3Mode3") DevCfg.LED3_Mod = 0x03;
			AddDataBuffer(new List<byte[]> { DevCfg.FlashCode[0xF, 0] });
		}
        #endregion
        #region 通信功能
        // 监听USB消息，捕获USB设备接入
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x219)
            {
                if (this.Text.Contains("Rev")) return;
                delayTime = 0;
                DelayWorker.Enabled = true;
            }
            base.WndProc(ref m);
        }
        private static int delayTime;
        // 设备接入后延时连接设备，防止USB接口连接不稳定
        private void DelayWorker_Tick(object sender, EventArgs e)
        {
            if (++delayTime == 5)
            {
                if (HidAPI.LinkOsuKeypad()) DeviceConnected();
                DelayWorker.Enabled = false;
            }
            Debug.WriteLine("DelayWorker: " + delayTime);
        }
        // 设备断开事件
        private void HidDeviceRemoved(object sender, EventArgs e)
        {
            if (BackPanel.InvokeRequired)
            {
                this.Invoke(new EventHandler(HidDeviceRemoved));
                return;
            }
            BackPanel.Enabled = false;
            foreach (Form child in Application.OpenForms)   // 关闭所有子窗口
            {
                if (child.Name != "FormMain") child.Close();
            }
            UIchange(-1);
        }
        // 设备接入事件
        private void DeviceConnected()
        {
            this.Text = "AmusingKeypad Rev:" + DeviceVer;
            UIchange(DeviceVer);
            OsuKeyboard_GetConfig();
        }
        // 获取osu!keypad基本配置
        private void OsuKeyboard_GetConfig()
        {
            sendDataBuffer.Clear();
            List<byte[]> getConfigList = new List<byte[]>();
            for (uint addrH = 0x11; addrH < 0x20; addrH++)
            {
                for (uint i = 0; i < 16; i++)
                {
                    uint addrL = i * 16;
                    getConfigList.Add(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, (byte)addrH, (byte)addrL });
                }
            }
            AddDataBuffer(getConfigList);
        }
        // 数据发送缓冲区
        private Queue sendDataBuffer = new Queue();
		// 批量添加数据到发送缓冲区
		public void AddDataBuffer(List<byte[]> buffers)
		{
			if (sendDataBuffer.Count > 0)
			{
				buffers.ForEach((buffer) => { sendDataBuffer.Enqueue(buffer); });
			}
			else
			{
				buffers.ForEach((buffer) => { sendDataBuffer.Enqueue(buffer); });
				IObuffer(new byte[] { 0x00 });
			}
		}
		/// <summary>
		/// HID设备数据到达事件
		/// </summary>
		private void HidDataReceived(object sender, EventArgs e)
		{
			byte[] readBuffer = ((Report)e).reportData;					// 缓存收到的数据
			if (readBuffer != null && readBuffer.Length == 39)			// 转存收到的数据
			{
				readBuffer[2] = 0x01;									// 标记数据，方便后续处理
				DevCfg.FlashCode[readBuffer[5] & 0x0F, readBuffer[6] >> 4] = readBuffer;
			}
			IObuffer(readBuffer);								// 收到数据，通知发送缓冲区
		}
		// 数据收发处理函数
		private void IObuffer(byte[] Rbytes)
		{
			if (HidAPI.sendReportLength == 0) return;
			if (sendDataBuffer.Count > 0)
			{
				try
				{
					HidAPI.SendBytes((byte[])sendDataBuffer.Dequeue());
				}
				catch (Exception ex)
				{
					Debug.Print("FileStream写错误 => " + ex.Message);
					sendDataBuffer.Clear();
				}
			}
			else
			{
				AnalyzeData();									// 数据接收完毕,调用委托分析处理数据
			}
		}
		/// <summary>
		/// 分析转存的数据
		/// </summary>
		private void AnalyzeData()
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new Action(AnalyzeData));
			}
			for (uint AddrH = 1; AddrH < 16; AddrH++)
			{
				for (uint AddrL = 0; AddrL < 16; AddrL++)
				{
					if (DevCfg.FlashCode[AddrH, AddrL][2] == 0x01)
					{
						DevCfg.FlashCode[AddrH, AddrL][0] = 0x81;
						DevCfg.FlashCode[AddrH, AddrL][2] = 0x00;       // 标记已处理数据
						ProcessData(AddrH, AddrL);
					}
					if (DevCfg.FlashCode[AddrH, AddrL][1] == 0xFF)
					{
						// 调试代码位，暂未用
					}
				}
			}
		}
		/// <summary>
		/// 处理转存的数据
		/// </summary>
		/// <param name="AddrH">地址高位</param>
		/// <param name="AddrL">地址低位</param>
		private void ProcessData(uint AddrH, uint AddrL)
		{
			if (AddrH == 3 && AddrL == 0)
			{
				LColor.BackColor = DevCfg.LED1_Color;
			}
			if (AddrH == 6 && AddrL == 0)
			{
				RColor.BackColor = DevCfg.LED2_Color;
			}
			if (AddrH == 9 && AddrL == 0)
			{
				MColor.BackColor = DevCfg.LED3_Color;
			}
			if (AddrH == 0xA && AddrL == 0)
			{
				string comkey = HidKeyCode.ConverComcode(DevCfg.Key1_Word[1], DeviceVer);
				string key = HidKeyCode.FindKeyFromCodes(HidKeyCode.HidKeypadCode, DevCfg.Key1_Word[0]);
				string KeyCode = key == "None" ? comkey.Substring(0, comkey.Length - 3) : comkey + key.ToUpper();
				toolTip.SetToolTip(Button1Code, KeyCode);
				toolTip.SetToolTip(Button1, KeyCode);
				Button1Code.Text = KeyCode;
			}
			if (AddrH == 0xB && AddrL == 0)
			{
				string comkey = HidKeyCode.ConverComcode(DevCfg.Key2_Word[1], DeviceVer);
				string key = HidKeyCode.FindKeyFromCodes(HidKeyCode.HidKeypadCode, DevCfg.Key2_Word[0]);
				string KeyCode = key == "None" ? comkey.Substring(0, comkey.Length - 3) : comkey + key.ToUpper();
				toolTip.SetToolTip(Button2Code, KeyCode);
				toolTip.SetToolTip(Button2, KeyCode);
				Button2Code.Text = KeyCode;
			}
			if (AddrH == 0xC && AddrL == 0)
			{
				string comkey = HidKeyCode.ConverComcode(DevCfg.Key3_Word[1], DeviceVer);
				string key = HidKeyCode.FindKeyFromCodes(HidKeyCode.HidKeypadCode, DevCfg.Key3_Word[0]);
				string KeyCode = key == "None" ? comkey.Substring(0, comkey.Length - 3) : comkey + key.ToUpper();
				toolTip.SetToolTip(Button3Code, KeyCode);
				toolTip.SetToolTip(Button3, KeyCode);
				Button3Code.Text = KeyCode;
			}
			if (AddrH == 0xD && AddrL == 0)
			{
				string comkey = HidKeyCode.ConverComcode(DevCfg.Key4_Word[1], DeviceVer);
				string key = HidKeyCode.FindKeyFromCodes(HidKeyCode.HidKeypadCode, DevCfg.Key4_Word[0]);
				string KeyCode = key == "None" ? comkey.Substring(0, comkey.Length - 3) : comkey + key.ToUpper();
				toolTip.SetToolTip(Button4Code, KeyCode);
				toolTip.SetToolTip(Button4, KeyCode);
				Button4Code.Text = KeyCode;
			}
			if (AddrH == 0xE && AddrL == 0)
			{
				string comkey = HidKeyCode.ConverComcode(DevCfg.Key5_Word[1], DeviceVer);
				string key = HidKeyCode.FindKeyFromCodes(HidKeyCode.HidKeypadCode, DevCfg.Key5_Word[0]);
				string KeyCode = key == "None" ? comkey.Substring(0, comkey.Length - 3) : comkey + key.ToUpper();
				toolTip.SetToolTip(Button5Code, KeyCode);
				toolTip.SetToolTip(Button5, KeyCode);
				Button5Code.Text = KeyCode;
			}
			if (AddrH == 0xF && AddrL == 0)
			{
				if (DevCfg.LED1_Mod == 0xFF) L1Mode0.Checked = true;
				if (DevCfg.LED1_Mod == 0x01) L1Mode1.Checked = true;
				if (DevCfg.LED1_Mod == 0x02) L1Mode2.Checked = true;
				if (DevCfg.LED1_Mod == 0x03) L1Mode3.Checked = true;
				if (DevCfg.LED2_Mod == 0xFF) L2Mode0.Checked = true;
				if (DevCfg.LED2_Mod == 0x01) L2Mode1.Checked = true;
				if (DevCfg.LED2_Mod == 0x02) L2Mode2.Checked = true;
				if (DevCfg.LED2_Mod == 0x03) L2Mode3.Checked = true;
				if (DevCfg.LED3_Mod == 0xFF) L3Mode0.Checked = true;
				if (DevCfg.LED3_Mod == 0x03) L3Mode3.Checked = true;
			}
			if (AddrH == 0xF && AddrL == 1)
			{
				BackPanel.Enabled = true;
				Debounce.Value = DevCfg.KeyTick > 200 ? 3 : DevCfg.KeyTick;
			}
		}
        #endregion
        #region 调试功能
        // 焦点在窗口上时的键盘按下事件
        private void FormMain_KeyDown(object sender, KeyEventArgs e)
		{
			// 后台忙时拦截调试功能的触发
			if (sendDataBuffer.Count > 0)
			{
				if (e.KeyCode == Keys.F5 || e.KeyCode == Keys.F6 || e.KeyCode == Keys.F12)
				{
					MessageBox.Show("请等待后台任务完成后再执行此命令！");
				}
				return;
			}
			if (BackPanel.Enabled == false) return;

            // 调试功能,用于检测触盘灯光是否正常
            if (e.KeyCode == Keys.F5)
            {
                tabControl.SelectedIndex = 1;
                if (LColor.BackColor == Color.FromArgb(0xFF, 0x00, 0x00) &&
                    RColor.BackColor == Color.FromArgb(0x00, 0xFF, 0x00) &&
                    MColor.BackColor == Color.FromArgb(0x00, 0x00, 0xFF))
                {
					List<byte[]> dataList = new List<byte[]>
					{
						new byte[] { 0x81, 0xFF, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x34, 0xFF, 0x34, 0xFF, 0x34, 0xFF, 0x34, 0xC0, 0x34, 0xC0, 0x34, 0xC0 }
					};
					AddDataBuffer(dataList);
                    SetColor(LColor, Color.FromArgb(0x00, 0x20, 0x00));
                    SetColor(RColor, Color.FromArgb(0x00, 0x00, 0x20));
                    SetColor(MColor, Color.FromArgb(0x20, 0x00, 0x00));
                }
                if (LColor.BackColor == Color.FromArgb(0x00, 0x20, 0x00) &&
                    RColor.BackColor == Color.FromArgb(0x00, 0x00, 0x20) &&
                    MColor.BackColor == Color.FromArgb(0x20, 0x00, 0x00))
                {
                    SetColor(LColor, Color.FromArgb(0x00, 0x00, 0x20));
                    SetColor(RColor, Color.FromArgb(0x20, 0x00, 0x00));
                    SetColor(MColor, Color.FromArgb(0x00, 0x20, 0x00));
                }
                if (LColor.BackColor == Color.FromArgb(0x00, 0x00, 0x20) &&
                    RColor.BackColor == Color.FromArgb(0x20, 0x00, 0x00) &&
                    MColor.BackColor == Color.FromArgb(0x00, 0x20, 0x00))
                {
                    SetColor(LColor, Color.FromArgb(0x20, 0x00, 0x00));
                    SetColor(RColor, Color.FromArgb(0x00, 0x20, 0x00));
                    SetColor(MColor, Color.FromArgb(0x00, 0x00, 0x20));
                }
                if (LColor.BackColor == Color.FromArgb(0x20, 0x00, 0x00) &&
                    RColor.BackColor == Color.FromArgb(0x00, 0x20, 0x00) &&
                    MColor.BackColor == Color.FromArgb(0x00, 0x00, 0x20))
                {
                    SetColor(LColor, Color.FromArgb(0x00, 0x20, 0x00));
                    SetColor(RColor, Color.FromArgb(0x00, 0x00, 0x20));
                    SetColor(MColor, Color.FromArgb(0x20, 0x00, 0x00));
                }
            }
			
			// 调试功能,复位键盘灯光到出厂值
            if (e.KeyCode == Keys.F6)
            {
                tabControl.SelectedIndex = 1;
				List<byte[]> dataList = new List<byte[]>
				{
					new byte[] {
						0x81, 0xFF, 0x00, 0x00, 0x00, 0x1F, 0x00,
						0x34, 0x03, 0x34, 0x03, 0x34, 0x03,
						0x34, 0xC0, 0x34, 0xC0, 0x34, 0xC0 }
				};
				if (DeviceVer < 3) for (int i = 0; i < 3; i++) dataList[0][8 + i * 2] = 0xFF;	// 此行代码仅对旧版触盘生效
				AddDataBuffer(dataList);
				SetColor(LColor, Color.FromArgb(0xFF, 0x00, 0x00));
                SetColor(RColor, Color.FromArgb(0x00, 0xFF, 0x00));
                SetColor(MColor, Color.FromArgb(0x00, 0x00, 0xFF));
            }

			// 以下功能旧版本触盘不支持，这里给屏蔽掉.
			if (DeviceVer < 3) return;

			// 调试功能,写入默认灯光色表
			if (e.KeyCode == Keys.F12)
            {
				// 设置默认灯光配置
				List<byte[]> dataList = new List<byte[]>
				{
					new byte[] {
						0x81, 0xFF, 0x00, 0x00, 0x00, 0x1F, 0x00,
						0x34, 0x03, 0x34, 0x03, 0x34, 0x03,
						0x34, 0xC0, 0x34, 0xC0, 0x34, 0xC0 }
				};
                // 载入默认颜色表数据
                for (int a = 0; a < 9; a++)                         // DefCfg.colorAddr.Count = 9
				{
					for (int i = 0; i < 12; i++)                    // DefCfg.colorTable.Count = 12
					{
						List<byte> sendColor = new List<byte>();
						sendColor.AddRange(DefCfg.colorAddr[a]);
						sendColor[sendColor.Count - 1] = (byte)(0x10 * i);
						sendColor.AddRange(DefCfg.colorTable[(i + a * 4) % 12]);
						dataList.Add(sendColor.ToArray());
					}
				}
				AddDataBuffer(dataList);
			}
        }
        // 焦点在窗口上时的键盘弹起事件
        private void FormMain_KeyUp(object sender, KeyEventArgs e)
        {
        }
		#endregion
	}
}
